﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    class IdNameService : IIdNameService
    {
        // 添加数据字典类型
        public long AddNew(IdNameDTO idName)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<IdName> bs = new BaseService<IdName>(ctx);
                var data = bs.GetAll().Any(i => i.TypeName == idName.TypeName && i.Name == idName.Name);
                if (data)
                {
                    throw new ArgumentException($"字典类型名为{idName.Name}已存在");
                }

                IdName dicIdName = new IdName();
                dicIdName.Name = idName.Name;
                dicIdName.TypeName = idName.TypeName;
                ctx.IdNames.Add(dicIdName);
                ctx.SaveChanges();
                return dicIdName.Id;
            }
        }

        // 获取所有的数据字典类型
        public IdNameDTO[] GetAll()
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<IdName> bs = new BaseService<IdName>(ctx);
                return bs.GetAll().ToList().Select(i => ToDTO(i)).ToArray();
            }
        }

        // 通过id获取指定数据类型的值
        public IdNameDTO GetById(long idNameId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<IdName> bs = new BaseService<IdName>(ctx);
                var data = bs.GetById(idNameId);
                if (data == null)
                {
                    throw new ArgumentException($"id为{idNameId}在数据字典表中不存在");
                }
                return ToDTO(data);
            }
        }

        // 根据数据字典类型获取数据字典
        public IdNameDTO[] GetNameByTypeName(string typeName)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<IdName> bs = new BaseService<IdName>(ctx);
                var data = bs.GetAll().Where(i => i.TypeName == typeName);
                if (data == null)
                {
                    throw new ArgumentException($"数据字典表中不存在类型名为{typeName}的数据");
                }

                return data.ToList().Select(i => ToDTO(i)).ToArray();
            }
        }

        public void MakeDelete(long idNameId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<IdName> bs = new BaseService<IdName>(ctx);
                bs.MakeDelete(idNameId);
            }
        }

        public void Updata(IdNameDTO idName)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<IdName> bs = new BaseService<IdName>(ctx);
                var data = bs.GetById(idName.Id);
                if (data == null)
                {
                    throw new ArgumentException($"字典数据表中不存在名字为{idName.Name}的数据");
                }


                data.CreateDateTime = idName.CreateDateTime;
                data.Id = idName.Id;
                data.Name = idName.Name;
                data.TypeName = idName.TypeName;

                ctx.SaveChanges();
            }
        }

        public IdNameDTO ToDTO(IdName idName)
        {
            IdNameDTO dto = new IdNameDTO();
            dto.CreateDateTime = idName.CreateDateTime;
            dto.Id = idName.Id;
            dto.Name = idName.Name;
            dto.TypeName = idName.TypeName;
            return dto;
        }

        public IdNameDTO GetIdByTypeNameAndName(string typeName, string name)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<IdName> bs = new BaseService<IdName>(ctx);
                var idname = bs.GetAll().SingleOrDefault(e => e.TypeName == typeName && e.Name == name);
                return idname == null ? null : ToDTO(idname);
            }
        }
    }
}
